package com.loadmore.baselibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunleilei on 2017/3/7
 */

public abstract class RVBaseAdapter extends RecyclerView.Adapter<RVBaseHolder> {
    protected Context context;
    public List items;
    public boolean order;
    protected LayoutInflater inflater;
    public abstract RVBaseHolder createHolder(ViewGroup parent, int viewType);
    public abstract void bindHolder(RVBaseHolder holder, int position);
    public RVBaseAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        items = new ArrayList();
    }

    public void append(List items) {
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void appendItem(Object object) {
        this.items.add(object);
        notifyDataSetChanged();
    }

    public void appendItem(Object object,int position) {
        this.items.add(position,object);
        notifyItemInserted(position);
    }

    public void append2Head(Object object) {
        this.items.add(0,object);
        notifyItemInserted(0);
        notifyDataSetChanged();
    }

    public void remove(Object object) {
        this.items.remove(object);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        this.items.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public RVBaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return createHolder(parent,viewType);
    }

    @Override
    public void onBindViewHolder(RVBaseHolder holder, int position) {
        bindHolder(holder,holder.getLayoutPosition());
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return items.get(position);
    }
}
